package com.xkdx.caipiao.hall;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import android.app.Service;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.listener.OnBallClickListener;
import com.xkdx.caipiao.listener.ShakeListener;
import com.xkdx.caipiao.listener.ShakeListener.OnShakeListener;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.pubclass.SelectCode;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.CaipiaoUtil;
import com.xkdx.caipiao.wiget.WhiteBallButton;

public class ArrangeThreeActivity extends CaiPiaoActivity implements
		OnBallClickListener {

	View baiView, shiView, geView;
	TextView bai, shi, ge;
	ShakeListener mShakeListener;
	int baiNum, shiNum, geNum;
	Vibrator mVibrator01;
	TextView mutiply_tv;
	int num = 0;
	int money = 0;
	String singleOrDouble = "单式";
	public List<String> baiLs = new ArrayList<String>();
	public List<String> shiLs = new ArrayList<String>();
	public List<String> geLs = new ArrayList<String>();
	private PopupWindow popupWindow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_arrange_3_caipiao);
		clearNumbersList();
		init();

	}

	private void clearNumbersList() {
		baiLs.clear();
		shiLs.clear();
		geLs.clear();
	}

	public void onBackClick(View view) {
		finish();
	}

	public void onRightClick(View view) {
		Intent intent = new Intent(this, ArrangeThreeRuleActivity.class);
		startActivity(intent);
	}

	public void onBetClick(View view) {
		IsIntent();
	}

	private void IsIntent() {
		if (baiLs.size() == 0 && shiLs.size() == 0 && geLs.size() == 0) {
			produceNumbers();

			setMutiplyList();

		} else {
			if (baiLs.size() < 1 || shiLs.size() < 1 || geLs.size() < 1) {
				Toast.makeText(getApplicationContext(), "请选择至少一注", 0).show();
			} else {
				getCaiPiaoObject(baiLs, shiLs, geLs);

				Intent intent = new Intent(this, BetThreeActivity.class);
				intent.putExtra("title", getString(R.string.arrange_3));
				startActivity(intent);
				finish();
			}
		}
	}

	private void init() {
		TextView tv_right = (TextView) findViewById(R.id.top_right_tv);
		tv_right.setText(getResources().getString(R.string.game_rule));
		mutiply_tv = (TextView) findViewById(R.id.mutiply_tv);
		baiView = (View) findViewById(R.id.bai);
		shiView = (View) findViewById(R.id.shi);
		geView = (View) findViewById(R.id.ge);

		bai = (TextView) baiView.findViewById(R.id.ball_million);
		shi = (TextView) shiView.findViewById(R.id.ball_million);
		ge = (TextView) geView.findViewById(R.id.ball_million);

		bai.setText("百位");
		shi.setText("十位");
		ge.setText("个位");

		markBalls(baiView, CaipiaoUtil.BAI);
		markBalls(shiView, CaipiaoUtil.SHI);
		markBalls(geView, CaipiaoUtil.GE);

		mShakeListener = new ShakeListener(this);
		mShakeListener.setOnShakeListener(new shakeLitener());

	}

	private class shakeLitener implements OnShakeListener {
		@Override
		public void onShake() {
			produceNumbers();
			setMutiplyList();
			mVibrator01 = (Vibrator) getApplication().getSystemService(
					Service.VIBRATOR_SERVICE);
			mVibrator01.vibrate(new long[] { 100, 10, 100, 100 }, -1);

		}

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mShakeListener != null) {
			mShakeListener.stop();
		}
	}

	private void produceNumbers() {
		clearNumbersList();
		Random random = new Random();
		baiNum = random.nextInt(9) + 1;
		shiNum = random.nextInt(9) + 1;
		geNum = random.nextInt(9) + 1;
		baiLs.add(String.valueOf(baiNum));
		shiLs.add(String.valueOf(shiNum));
		geLs.add(String.valueOf(geNum));
		findNumbeText(baiView, baiNum);
		findNumbeText(shiView, shiNum);
		findNumbeText(geView, geNum);

	}

	private void findNumbeText(View view, int num) {
		WhiteBallButton tv00 = (WhiteBallButton) view
				.findViewById(R.id.ball_00);
		WhiteBallButton tv01 = (WhiteBallButton) view
				.findViewById(R.id.ball_01);
		WhiteBallButton tv02 = (WhiteBallButton) view
				.findViewById(R.id.ball_02);
		WhiteBallButton tv03 = (WhiteBallButton) view
				.findViewById(R.id.ball_03);
		WhiteBallButton tv04 = (WhiteBallButton) view
				.findViewById(R.id.ball_04);
		WhiteBallButton tv05 = (WhiteBallButton) view
				.findViewById(R.id.ball_05);
		WhiteBallButton tv06 = (WhiteBallButton) view
				.findViewById(R.id.ball_06);
		WhiteBallButton tv07 = (WhiteBallButton) view
				.findViewById(R.id.ball_07);
		WhiteBallButton tv08 = (WhiteBallButton) view
				.findViewById(R.id.ball_08);
		WhiteBallButton tv09 = (WhiteBallButton) view
				.findViewById(R.id.ball_09);

		tv00.setState("" + num);
		tv01.setState("" + num);
		tv02.setState("" + num);
		tv03.setState("" + num);
		tv04.setState("" + num);
		tv05.setState("" + num);
		tv06.setState("" + num);
		tv07.setState("" + num);
		tv08.setState("" + num);
		tv09.setState("" + num);

	}

	private void markBalls(View view, String mark) {
		WhiteBallButton tv00 = (WhiteBallButton) view
				.findViewById(R.id.ball_00);
		WhiteBallButton tv01 = (WhiteBallButton) view
				.findViewById(R.id.ball_01);
		WhiteBallButton tv02 = (WhiteBallButton) view
				.findViewById(R.id.ball_02);
		WhiteBallButton tv03 = (WhiteBallButton) view
				.findViewById(R.id.ball_03);
		WhiteBallButton tv04 = (WhiteBallButton) view
				.findViewById(R.id.ball_04);
		WhiteBallButton tv05 = (WhiteBallButton) view
				.findViewById(R.id.ball_05);
		WhiteBallButton tv06 = (WhiteBallButton) view
				.findViewById(R.id.ball_06);
		WhiteBallButton tv07 = (WhiteBallButton) view
				.findViewById(R.id.ball_07);
		WhiteBallButton tv08 = (WhiteBallButton) view
				.findViewById(R.id.ball_08);
		WhiteBallButton tv09 = (WhiteBallButton) view
				.findViewById(R.id.ball_09);

		tv00.setMark(mark);
		tv01.setMark(mark);
		tv02.setMark(mark);
		tv03.setMark(mark);
		tv04.setMark(mark);
		tv05.setMark(mark);
		tv06.setMark(mark);
		tv07.setMark(mark);
		tv08.setMark(mark);
		tv09.setMark(mark);

		tv00.setBallClickListener(this);
		tv01.setBallClickListener(this);
		tv02.setBallClickListener(this);
		tv03.setBallClickListener(this);
		tv04.setBallClickListener(this);
		tv05.setBallClickListener(this);
		tv06.setBallClickListener(this);
		tv07.setBallClickListener(this);
		tv08.setBallClickListener(this);
		tv09.setBallClickListener(this);
		
		

	}

	public void setMutiplyList() {
		int bai = baiLs.size(), shi = shiLs.size(), ge = geLs.size();
		num = bai * shi * ge;
		money = num * 2;
		singleOrDouble = num > 1 ? "复式" : "单式";
		if (num == 0) {
			mutiply_tv.setText("");
		} else {
			mutiply_tv.setText("共" + num + "注" + " " + money + "元");
		}
	}

	public void clearNumber(View view) {
		clearNumbersList();
		findNumbeText(baiView, -1);
		findNumbeText(shiView, -1);
		findNumbeText(geView, -1);

	}

	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {

	}

	@Override
	public void startThread() {

	}

	private void getCaiPiaoObject(List<String> bais, List<String> shis,
			List<String> ges) {
		StringBuffer sb = new StringBuffer();

		SelectCode code = new SelectCode();

		for (String bai : bais) {

			sb.append(bai);
		}

		sb.append(" ");
		for (String shi : shis) {

			sb.append(shi);
		}

		sb.append(" ");
		for (String ge : ges) {

			sb.append(ge);
		}

		code.setRedCode(sb.toString());

		code.setMoneyPerBet(money + "");
		code.setBettingNum(num + "");
		code.setSingleOrDouble(singleOrDouble);
		code.setCaizhong(getResources().getString(R.string.arrange_3));
		code.setBetMode(singleOrDouble + " " + mutiply_tv.getText().toString());
		IConstants.list_three.add(code);

	}

	@Override
	public void RemoveBall(String type, String text) {
		if (type.equals(CaipiaoUtil.BAI)) {
			baiLs.remove(text);
		} else if (type.equals(CaipiaoUtil.SHI)) {
			shiLs.remove(text);
		} else {
			geLs.remove(text);
		}
	}

	@Override
	public void AddBall(String type, String text) {
		if (type.equals(CaipiaoUtil.BAI)) {
			baiLs.add(text);
		} else if (type.equals(CaipiaoUtil.SHI)) {
			shiLs.add(text);
		} else {
			geLs.add(text);
		}
	}

	@Override
	public void ShowBallPopoWidow(View v,String text) {
		View view = getLayoutInflater().inflate(R.layout.popwindow_caipiao, null);

		popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		TextView tv = (TextView) view.findViewById(R.id.text);
		tv.setText(text);
		
		int[] location = new int[2];
		v.getLocationOnScreen(location);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(true);
		int left = v.getLeft();
		int right = v.getRight();
		int top = v.getTop();
		int bottom = v.getBottom();

		int vWidth = (int) ((right - left) * 0.5);
		popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] - vWidth
				+ 4, location[1] - (int) (1.6 * (bottom - top))-10);
	}

	@Override
	public void DismissBallPopowindow() {
		if(popupWindow!=null){
			popupWindow.dismiss();
		}
		
	}

}
